/*
 * Copyright © 2022-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springcloud.seata.auth.config;

import cn.denvie.springcloud.seata.auth.model.vo.AccountDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * JwtTokenEnhancer。
 *
 * @author Denvie
 * @date 2022/4/18
 * @since 1.0.0
 */
public class JwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        AccountDetails account = (AccountDetails) authentication.getPrincipal();
        final Map<String, Object> additionalInfo = new HashMap<>(2);
        additionalInfo.put("userId", account.getAccount().getUserId());
        additionalInfo.put("nickname", account.getAccount().getNickname());

        final Map<String, Object> resultMap = new HashMap<>(1);
        resultMap.put("additionalInfo", additionalInfo);

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(resultMap);

        return accessToken;
    }
}
